package com.itcs.rongyun.rongsdk;
import java.util.HashMap; 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 

@SuppressWarnings("serial")
public class Response extends HashMap<String,Object> {
	
	private static String CODE = "code";
	private static String DATA = "data";
	private static String TOTAL = "total";
	private static String MESSAGE = "message";
	private Logger logger = LoggerFactory.getLogger(Response.class); 
	
	private Response() {
		this.put(CODE, Codes.SUCCESS);
	}
	
	public static Response newResponse() {
		return new Response();
	}
	
	public static Response set(String key, Object value) {
		Response response = Response.newResponse();
		response.put(key, value);
		return response;
	}
	
	public static Response set(Object data) {
		Response response = Response.newResponse();
		response.put(DATA, data);
		response.put(CODE, Codes.SUCCESS);
		response.put(MESSAGE, "成功!");
		return response;
	}
	
	public static Response set(Object data,String code) {
		Response response = Response.newResponse();
		response.put(DATA, data); 
		response.put(CODE, code);
		return response;
	}


	public void setData(Object data){
		this.put(DATA, data);
	}
	
	public Response put(String key, Object value) {
		super.put(key, value);
		return this;
	}
	
	public Response moveTo(String fromKey, String toKey) {
		Object val = this.get(fromKey);
		this.put(toKey, val);
		this.remove(fromKey);
		return this;
	}
	
	public Boolean isOK() {
		return this.getCode() == Codes.SUCCESS;
	}
	
	public Boolean isFail() {
		return !isOK();
	}
	
	public Response ok(Object value) {
		super.put(DATA, value);
		return this;
	}
	
	public Response ok(String key, Object value) {
		super.put(key, value);
		return this;
	}
	
	public Response setResults(Integer total, Object rows) {
		this.setTotal(total);
		this.ok(rows);
		return this;
	}
	
	public Response results(Integer total, Object rows) {
		this.setTotal(total);
		this.ok(rows);
		return this;
	}
	
	private Response setCode(int code) {
		this.put(CODE, code);
		return this;
	}
	
	private static Response code(int code) {
		Response result = new Response();
		result.setCode(code);
		
		return result;
	}
	
	public int getCode() {
		return Integer.parseInt(this.get(CODE).toString());
	}
	
	public Response error(Exception e) {
		e.printStackTrace();
		logger.error("ExceptionError",e);
		logger.error("Exception",e.toString()+e.getLocalizedMessage()+e.getMessage());
		
		return Response.SERVER_ERROR();
	}
	
	public Response setTotal(Integer total) {
		this.put(TOTAL, total);
		return this;
	}
	
	public static Response set(Integer total, Object rows) {
		Response response = new Response();
		response.setTotal(total);
		response.ok(rows);
		
		return response;
	}
	
//============状态===================================================================================	
	
	public static Response OK() {
		Response response = Response.code(Codes.SUCCESS);
		response.put(MESSAGE, "成功");
		return response;
	}
	
	public static Response ERROR() {
		Response response = Response.code(Codes.ERROR);
		response.put(MESSAGE, "失败");
		return response;
	}
	
	public static Response SERVER_ERROR() {
		Response response = Response.code(Codes.SERVER_ERROR);
		response.put(MESSAGE, "server error");
		return response;
	}
    
	public static Response MOBILE_EXISTS() {
		Response response = Response.code(Codes.MOBILE_EXISTS);
		response.put(MESSAGE, "mobile is exists!");
		return response;
	}
	
	public static Response MOBILE_NOT_EXISTS() {
		Response response = Response.code(Codes.MOBILE_NOT_EXISTS);
		response.put(MESSAGE, "mobile is not exists!");
		return response;
	}
	
	/**
	 * TOKEN失效
	 * @return
	 */
	public static Response TOKEN_EXPIRED(){
		Response response = Response.code(Codes.TOKEN_EXPIRED);
		response.put(MESSAGE, "token expried!");
		return response;
	}
	
	public static Response PARAM_ERROR() {
		Response response = Response.code(Codes.PARAM_ERROR);
		response.put(MESSAGE, "parameters is error!");
		return response;
	}
	
	public static Response SMS_EXPIRED(){
		Response response = Response.code(Codes.SMS_EXPIRED);
		response.put(MESSAGE, "sms expired");
		return response;
	}
	
	public static Response NAME_EXIST(){
		Response response = Response.code(Codes.NAME_EXIST);
		response.put(MESSAGE, "name exists");
		return response;
	}
	
    
	public static Response ACCOUNT_EXISTS() {
		Response response = Response.code(Codes.ACCOUNT_EXISTS);
		response.put(MESSAGE, "account exists");
		return response;
	}
	
	public static Response ACCOUNT_LOCKED() {
		Response response = Response.code(Codes.ACCOUNT_LOCKED);
		response.put(MESSAGE, "account locked");
		return response;
	}
	
	public static Response ACCOUNT_NOT_EXISTS() {
		Response response = Response.code(Codes.ACCOUNT_NOT_EXISTS);
		response.put(MESSAGE, "account not exists");
		return response;
	}
	
	public static Response ACCOUNT_PASS_ERROR() {
		Response response = Response.code(Codes.ACCOUNT_PASS_ERROR);
		response.put(MESSAGE, "account password error");
		return response;
	}
	
	public static Response PASSWORD_EMPTY() {
		Response response = Response.code(Codes.PASSWORD_EMPTY);
		response.put(MESSAGE, "password empty");
		return response;
	}
	
	public static Response PASSWORD_ERROR() {
		Response response = Response.code(Codes.PASSWORD_ERROR);
		response.put(MESSAGE, "password error");
		return response;
	}
	 
	
	
	public static  Response NOT_PERMISSION() {
		Response response = Response.code(Codes.NOT_PERMISSION);
		response.put(MESSAGE, "not permission!");
		return response;
	}
	 
	 
	
	public static Response STORE_NOT_EXIST(){
		Response response = Response.code(Codes.STORE_NOT_EXIST);
		response.put(MESSAGE, "store not exists");
		return response;
	}
	
	public static Response STORE_CODE_EXISTS(){
		Response response = Response.code(Codes.STORE_CODE_EXISTS);
		response.put(MESSAGE, "store not exists");
		return response;
	}
	 
	 
	
//===========================================APP错误代码=======================================================	
	
	/**
	 * 成功
	 * @return
	 */
	public static Response APP_OK() {
		Response response = Response.code(Codes.SUCCESS);
		response.put(MESSAGE, "成功");
		return response;
	}
	
	public static Response APP_SERVER_ERROR() {
		Response response = Response.code(Codes.SERVER_ERROR);
		response.put(MESSAGE, "server error");
		return response;
	}
    
	/**
	 * 手机号已存在
	 * @return
	 */
	public static Response APP_MOBILE_EXISTS() {
		Response response = Response.code(Codes.APP_MOBILE_EXISTS);
		response.put(MESSAGE, "手机号已存在");
		return response;
	}
	
	/**
	 * 手机号码不存在
	 * @return
	 */
	public static Response APP_MOBILE_NOT_EXISTS() {
		Response response = Response.code(Codes.APP_MOBILE_NOT_EXISTS);
		response.put(MESSAGE, "手机号码不存在");
		return response;
	}
	
	/**
	 * token失效
	 * @return
	 */
	public static Response APP_TOKEN_EXPIRED(){
		Response response = Response.code(Codes.APP_TOKEN_EXPIRED);
		response.put(MESSAGE, "token失效");
		return response;
	}
 
	/**
	 * 参数错误
	 * @return
	 */
	public static Response APP_PARAM_ERROR() {
		Response response = Response.code(Codes.APP_PARAM_ERROR);
		response.put(MESSAGE, "参数错误");
		return response;
	}
	
	

	/**
	 * 短信验证码错误
	 * @return
	 */
	public static Response APP_SMS_ERROR(){
		Response response = Response.code(Codes.APP_SMS_ERROR);
		response.put(MESSAGE, "短信验证码错误");
		return response;
	}

	/**
	 * 短信验证码失效
	 * @return
	 */
	public static Response APP_SMS_EXPIRED(){
		Response response = Response.code(Codes.APP_SMS_EXPIRED);
		response.put(MESSAGE, "短信验证码失效");
		return response;
	}
	
	/**
	 * 名称已经存
	 * @return
	 */
	public static Response APP_NAME_EXIST(){
		Response response = Response.code(Codes.APP_NAME_EXIST);
		response.put(MESSAGE, "名称已经存");
		return response;
	}
	
    /**
     * 帐号已存在
     * @return
     */
	public static Response APP_ACCOUNT_EXISTS() {
		Response response = Response.code(Codes.APP_ACCOUNT_EXISTS);
		response.put(MESSAGE, "帐号已存在");
		return response;
	}
	
	/**
	 * 帐号未注册
	 * @return
	 */
	public static Response APP_ACCOUNT_NOT_EXISTS() {
		Response response = Response.code(Codes.APP_ACCOUNT_NOT_EXISTS);
		response.put(MESSAGE, "帐号未注册");
		return response;
	}
	
	/**
	 * 帐号或密码不正确
	 */
	public static Response APP_ACCOUNT_PASS_ERROR() {
		Response response = Response.code(Codes.APP_ACCOUNT_PASS_ERROR);
		response.put(MESSAGE, "帐号或密码不正确");
		return response;
	} 
	
	/**
	 * 手机号码错误
	 */
	public static Response APP_MOBILE_ERROR() {
		Response response = Response.code(Codes.APP_MOBILE_ERROR);
		response.put(MESSAGE, "手机号码错误");
		return response;
	} 
	 
	/**
	 * 标签不存在
	 */
	public static Response APP_PASSWORD_ERROR() {
		Response response = Response.code(Codes.APP_PASSWORD_ERROR);
		response.put(MESSAGE, "密码错误");
		return response;
	} 
	
	/**
	 * 不是好友
	 */
	public static Response APP_NOT_FRIENDS() {
		Response response = Response.code(Codes.APP_NOT_FRIENDS);
		response.put(MESSAGE, "不是好友");
		return response;
	} 
	
    /**
     * 游记相册名称已存在
     */
	public static Response APP_ALBUM_NAME_EXIST() {
		Response response = Response.code(Codes.APP_ALBUM_NAME_EXIST);
		response.put(MESSAGE, "游记相册名称已存在");
		return response;
	} 
	
    /**
     * 游记相册名称已存在
     */
	public static Response APP_NOT_REPORT() {
		Response response = Response.code(Codes.APP_NOT_REPORT);
		response.put(MESSAGE, "游记相册名称已存在");
		return response;
	}  

    /**
     * 未登录用户操作提示，请先登录
     */
	public static Response APP_TOKEN_LOGIN_EXPIRED() {
		Response response = Response.code(Codes.APP_TOKEN_LOGIN_EXPIRED);
		response.put(MESSAGE, "未登录用户操作提示，请先登录");
		return response;
	}   
 
    /**
     * 库存不足
     */
	public static Response APP_COMMODITY_NOT_STOCK() {
		Response response = Response.code(Codes.APP_COMMODITY_NOT_STOCK);
		response.put(MESSAGE, "库存不足");
		return response;
	}   

    /**
     * 商品过期
     */
	public static Response APP_COMMODITY_TIME_EXPIRED() {
		Response response = Response.code(Codes.APP_COMMODITY_TIME_EXPIRED);
		response.put(MESSAGE, "商品过期");
		return response;
	}   

    /**
     * 余额不足
     */
	public static Response APP_COMMODITY_NOT_AMOUNT() {
		Response response = Response.code(Codes.APP_COMMODITY_NOT_AMOUNT);
		response.put(MESSAGE, "余额不足");
		return response;
	}   

    /**
     * 第三方账号已被绑定
     */
	public static Response APP_OAUTH_EXIST() {
		Response response = Response.code(Codes.APP_COMMODITY_NOT_AMOUNT);
		response.put(MESSAGE, "第三方帐号已被绑定");
		return response;
	}   
	
    /**
     * 车牌号已存在
     */
	public static Response APP_PLATE_EXIST() {
		Response response = Response.code(Codes.APP_PLATE_EXIST);
		response.put(MESSAGE, "车牌号已存在");
		return response;
	}   
	
    /**
     * 车牌号不存在
     */
	public static Response APP_PLATE_NOT_EXIST() {
		Response response = Response.code(Codes.APP_PLATE_NOT_EXIST);
		response.put(MESSAGE, "车牌号不存在");
		return response;
	}   
	
    /**
     * 您可以通行，通行费已支付啦！
     */
	public static Response APP_ORDER_PAYMENT() {
		Response response = Response.code(Codes.APP_ORDER_PAYMENT);
		response.put(MESSAGE, "您可以通行，通行费已支付啦！");
		return response;
	}   
	
    /**
     * 订单交易失败
     */
	public static Response APP_ORDER_FAILS() {
		Response response = Response.code(Codes.APP_ORDER_FAILS);
		response.put(MESSAGE, "订单交易失败");
		return response;
	}  
	
    /**
     * 订单已取消，请使用现金支付通行费！
     */
	public static Response APP_ORDER_CANCEL() {
		Response response = Response.code(Codes.APP_ORDER_CANCEL);
		response.put(MESSAGE, "订单已取消，请使用现金支付通行费！");
		return response;
	}   
	
    /**
     * 没有设备
     */
	public static Response APP_NOT_DEVICE() {
		Response response = Response.code(Codes.APP_NOT_DEVICE);
		response.put(MESSAGE, "附近没有可支持在线支付收费站哦");
		return response;
	}   
	
    /**
     * 没有在线设备
     */
	public static Response APP_NOT_DEVICE_STATUS() {
		Response response = Response.code(Codes.APP_NOT_DEVICE_STATUS);
		response.put(MESSAGE, "该收费站目前没有支持ITC");
		return response;
	}     
	

    /**
     * 该城市没有限行信息
     */
	public static Response APP_NOT_CITY_XIANXING() {
		Response response = Response.code(Codes.APP_NOT_CITY_XIANXING);
		response.put(MESSAGE, " 该城市没有限行信息");
		return response;
	}     
	

    /**
     * 安全码错误
     */
	public static Response APP_SECURITY_ERROR() {
		Response response = Response.code(Codes.APP_SECURITY_ERROR);
		response.put(MESSAGE, " 安全码错误");
		return response;
	}      
 
	/*************************************************************融云Code命令*****************************************************************/
	
	/**
     * 通行费支付
     */
	public static Response APP_NOTIFICATION_PAY(Object data) {
		Response response = Response.code(Codes.APP_NOTIFICATION_PAY);
		response.put(DATA, data);
		response.put(MESSAGE, "通行费用支付");
		return response;
	}   
	
	/**
     * 通行费支付成功
     */
	public static Response APP_NOTIFICATION_SUCCESS(Object data) {
		Response response = Response.code(Codes.APP_NOTIFICATION_SUCCESS);
		response.put(DATA, data);
		response.put(MESSAGE, "通行费支付成功");
		return response;
	}   
	
	/**
     * 通行费详情
     */
	public static Response APP_NOTIFICATION_DETAIL(Object data) {
		Response response = Response.code(Codes.APP_NOTIFICATION_DETAIL);
		response.put(MESSAGE, "通行费详情");
		response.put(DATA, data);
		return response;
	}   
	
	/**
     * 通行入口信息
     */
	public static Response APP_IN_TOLL(Object data) {
		Response response = Response.code(Codes.APP_IN_TOLL);
		response.put(MESSAGE, "通行入口信息");
		response.put(DATA, data);
		return response;
	}   
	
	/**
     * 充值成功通知
     */
	public static Response APP_NOTIFICATION_RECHARGE(Object data) {
		Response response = Response.code(Codes.APP_NOTIFICATION_RECHARGE);
		response.put(MESSAGE, "充值通知");
		response.put(DATA, data);
		return response;
	}     
	
	/**
     * 路方数据推送
     */
	public static Response APP_NOTIFICATION_TOLLDATA(Object data) {
		Response response = Response.code(Codes.APP_NOTIFICATION_TOLLDATA);
		response.put(MESSAGE, "路方数据推送");
		response.put(DATA, data);
		return response;
	}
	
	/**
     * 移动APP系统公告
     */
	public static Response APP_NOTIFICATION_ITC(Object data) {
		Response response = Response.code(Codes.APP_NOTIFICATION_ITC);
		response.put(MESSAGE, "移动APP系统公告");
		response.put(DATA, data);
		return response;
	}
	
	/**
     * WEB企业系统公告
     */
	public static Response APP_NOTIFICATION_ITCWEB(Object data) {
		Response response = Response.code(Codes.APP_NOTIFICATION_ITCWEB);
		response.put(MESSAGE, "路方数据推送");
		response.put(DATA, data);
		return response;
	}
}